// pages/activity/activity.js
const app = getApp()

Page({
  data: {
    showPointsModal: false,
    remote_src: app.globalData.remote_src,
    userInfo: {},
    activeTab: 'activity',
    points: 9,
    currentTab: 'wantToGo',
    wantToGoCount: 0,
    registeredCount: 0,
    createdCount: 0,
    finishedCount: 0,
    wantToGoList: [
    ],
    registeredList: [
  
    ],
    finishedList: [

    ],
    createdList: [
      
    ]
  },
  onLoad(options) {
    if (options.userInfo) {
      const userInfo = JSON.parse(decodeURIComponent(options.userInfo));
      // 如果存在编码后的 interests，需要解码
      if (typeof userInfo.interests === 'string') {
        userInfo.interests = JSON.parse(decodeURIComponent(userInfo.interests));
      }
      this.setData({
        userInfo
      });
      this.loadWantToGoList();
      this.loadCreatedList();
    }
  },
  // 切换顶部导航栏
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({
      activeTab: tab
    })
  },
  
  // 切换活动标签页
  changeTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({ currentTab: tab });
    // 根据不同标签加载对应数据
    switch(tab) {
      case 'wantToGo':
        this.loadWantToGoList();
        break;
      case 'registered':
        this.loadRegisteredList();
        break;
      case 'created':
        console.log('created')
        this.loadCreatedList();
        break;
      case 'finished':
        this.loadFinishedList();
        break;
    }
  },
  // 加载想去的活动列表
  loadWantToGoList() {
      const websiteUrl = getApp().globalData.onlineService + '/api/fav/fav';
      wx.request({
        url: websiteUrl,
        method: 'GET',
        header: {
          'content-type': 'application/json',
          'Authorization': `Bearer ${getApp().globalData.userInfo.token}`
        },
        data: {
          page: 1,
          page_size: 10
        },
        success: (res) => {
          console.log(res.data)
          if (res.data.code === 200) {
            // 转换数据结构以匹配前端需要的格式
            const formattedList = res.data.msg.list.map(item => ({
              id: item.activity_id,
              name: item.activity_name,
              content: item.activity_content,
              type: item.activity_type,
              images: item.activity_images,
              fee: item.activity_fee,
              status: item.activity_status,
              start_time: item.activity_start_time,
              end_time: item.activity_end_time,
              people_number: item.activity_people_number,
              people_age: item.activity_people_age,
              created_user_id: item.created_user_id,
              address: item.address,
              c_time: item.created_at
            }));
            
            this.setData({
              wantToGoList: formattedList,
              wantToGoCount: res.data.msg.total
            });
          } else {
            wx.showToast({
              title: res.data.msg || '获取收藏列表失败',
              icon: 'none'
            });
          }
        },
        fail: () => {
          wx.showToast({
            title: '网络请求失败',
            icon: 'none'
          });
        }
      });
  },
  // 加载已报名的活动列表
  loadRegisteredList() {
    const websiteUrl = getApp().globalData.onlineService + '/api/reg/reg';
    wx.request({
      url: websiteUrl,
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${getApp().globalData.userInfo.token}`
      },
      data: {
        page: 1,
        page_size: 10
      },
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            registeredList: res.data.msg.list,
            registeredCount: res.data.msg.total
          });
        } else {
          wx.showToast({
            title: res.data.msg || '获取报名列表失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },
  // 加载我发起的活动列表
  loadCreatedList() {
    const websiteUrl = getApp().globalData.onlineService + '/api/activity/activity';
    wx.request({
      url: websiteUrl,
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${getApp().globalData.userInfo.token}` 
      },
      data: {
        user_id: this.data.userInfo.id,
        page: 1,
        page_size: 10,
        status: '0,1'
      },
      success: (res) => {
        if (res.data.code === 200) {
          console.log(res.data.data)

          this.setData({
            createdList: res.data.data.activities,
            createdCount: res.data.data.total
          });
          console.log(this.data.createdList)
          
        }
      }
    });
  },
   // 加载已结束的活动列表
  loadFinishedList() {
    const websiteUrl = getApp().globalData.onlineService + '/api/activity/activity';
    console.log(websiteUrl, getApp().globalData.userInfo.token)
    wx.request({
      url: websiteUrl,
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${getApp().globalData.userInfo.token}` 
      },
      data: {
        user_id: this.data.userInfo.id,
        page: 1,
        page_size: 10,
        status: '2,3'
      },
      success: (res) => {
        if (res.data.code === 200) {
          console.log(res.data.data)

          this.setData({
            finishedList: res.data.data.activities,
            finishedCount: res.data.data.total
          });
          console.log(this.data.finishedList)
          
        }
      }
    });
  },
  goToActivityDetail(e) {
    const activityId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/home/activity/activity?id=${activityId}`,
      fail: (err) => {
        wx.showToast({
          title: '页面跳转失败',
          icon: 'none'
        });
      }
    });
  },
  // 查看更多积分
  viewMorePoints: function() {
    this.setData({
      showPointsModal: true
    });
  },
  closePointsModal() {
    this.setData({
      showPointsModal: false
    });
  },
  // 关注用户
  followUser: function() {
    wx.showToast({
      title: '关注成功',
      icon: 'success'
    })
  },
  
  // 获取活动列表
  getActivityList: function() {
    // 这里可以添加从服务器获取活动列表的代码
    // wx.request({
    //   url: app.globalData.offlineService + '/api/activities',
    //   method: 'GET',
    //   success: (res) => {
    //     if (res.data.status === 'success') {
    //       this.setData({
    //         wantToGoList: res.data.wantToGo,
    //         registeredList: res.data.registered,
    //         finishedList: res.data.finished,
    //         wantToGoCount: res.data.wantToGo.length,
    //         registeredCount: res.data.registered.length
    //       })
    //     }
    //   }
    // })
  },
  goToSettings: function() {
    wx.navigateTo({
      url: '/pages/mine/my_setting/my_setting',
    })
  },
})